﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCablePassNearFrameControlBinder
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApCablePassNearFrameControlBinder : IDisposable
{
  private bool m_isBindingInProgress;
  private readonly ICApCableEditBoxControl m_passNearFrameControl;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;

  public CApCablePassNearFrameControlBinder(
    ICApCableEditBoxControl passNearFrameControl,
    ICApFLEXCableAttributeViewModel attributeViewModel)
  {
    this.m_passNearFrameControl = passNearFrameControl ?? throw new ArgumentNullException(nameof (passNearFrameControl));
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_passNearFrameControl.DisplayableObject = this.m_attributeViewModel.PassNearFrame as ITxDisplayableObject;
    this.m_passNearFrameControl.ObjectChanged += new CApCableEditBoxControlEventHandler(this.PassNearFrameControl_ObjectChanged);
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_isBindingInProgress = false;
  }

  public void Dispose()
  {
    this.m_passNearFrameControl.ObjectChanged -= new CApCableEditBoxControlEventHandler(this.PassNearFrameControl_ObjectChanged);
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (this.m_isBindingInProgress || !(e.PropertyName == "PassNearFrame"))
      return;
    this.m_isBindingInProgress = true;
    this.m_passNearFrameControl.DisplayableObject = this.m_attributeViewModel.PassNearFrame as ITxDisplayableObject;
    this.m_isBindingInProgress = false;
  }

  private void PassNearFrameControl_ObjectChanged(ICApCableEditBoxControl sender)
  {
    if (this.m_isBindingInProgress)
      return;
    this.m_isBindingInProgress = true;
    this.m_attributeViewModel.PassNearFrame = this.m_passNearFrameControl.DisplayableObject as ITxLocatableObject;
    this.m_isBindingInProgress = false;
  }
}
